/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef WX_INTERFACE_CARD_H
#define WX_INTERFACE_CARD_H

#include <mc/engine.h>
#include <mc/core/timer.h>
#include <ncsi_over_mctp/ncsi_over_mctp_wx.h>
#include <imu/imu.h>

#include "interface/board.h"
#include "interface/network_adapter.h"
#include "interface/network_adapter/cooling.h"
#include "interface/pcie_card.h"
#include "interface/pcie_device.h"
#include "wx_port.h"

namespace dev {
using ncsi_over_mctp_wx_ptr = std::shared_ptr<ncsi_over_mctp_wx>;
using imu_ptr               = std::shared_ptr<imu>;

class wx_card : public mc::engine::object<wx_card> {
public:
    MC_OBJECT(
        wx_card, "PCIeNicCard", "/bmc/dev/Systems/1/PCIeNicCard/${object_name}",
        (PCIeDevice)(PCIeCard)(NetworkAdapter_Cooling)(Board)(NetworkAdapter))
    wx_card();
    ~wx_card();
    bool start();
    bool stop();
    bool init(mc::mutable_dict& csr_object, const mc::dict& connector);
    void init_network_ports();
    void update_ports_bdf_async(imu_pcie_device_req_t& pcie_device_info);
    void replace_dynamic_property(mc::mutable_dict& object, const mc::dict& connector);

    // === network adapter quadruple ===
    bool get_network_adapter_quadruple();
    bool get_pcie_vid_did_info(uint16_t& vendor_id, uint16_t& device_id);
    bool get_pcie_svid_sdid_info(uint16_t& subsystem_vendor_id, uint16_t& subsystem_device_id);
    void update_quadruple_info_async();
    bool is_system_power_on();

    PCIeDevice             m_pcie_device;
    PCIeCard               m_pcie_card;
    NetworkAdapter_Cooling m_network_adapter_cooling;
    NetworkAdapter         m_network_adapter;
    Board                  m_board;
    uint8_t                m_system_id;

private:
    std::vector<wx_port*> m_network_ports;

    // ncsi over mctp 实现
private:
    bool                  start_ncsi_protocol();
    bool                  start_protocol();
    bool                  init_imu_protocol();
    void                  on_quadruple_update_timer();
    ncsi_over_mctp_wx_ptr m_ncsi_over_mctp_wx;
    imu_ptr               m_imu_obj;
    mc::milliseconds      m_interval = mc::milliseconds(5000);  // ncsi over mctp 更新间隔5s
    mc::milliseconds      m_quadruple_interval = mc::milliseconds(10000); // 四元组更新间隔10s
    mc::core::timer*      m_quadruple_timer; // 四元组更新定时器
    bool                  m_first_quadruple_update; // 是否首次四元组更新
};

} // namespace dev

#endif // __WX_CARD_H__